package com.cy.pj.activity.service.impl;

import com.cy.pj.activity.dao.ActivityDao;
import com.cy.pj.activity.pojo.Activity;
import com.cy.pj.activity.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityDao activityDao;
    @Override
    public List<Activity> findActivitys() {
        return  activityDao.findActivitys();
    }

    @Override
    public int saveActivity(Activity entity) {
        int rows=activityDao.insertObject(entity);
        Timer timer=new Timer();
        //此对象可以负责去执行一些任务(这个对象内置一个线程和一个任务队列)
        //启动线程执行任务调度
        timer.schedule(new TimerTask() {//TimerTask为任务
            @Override
            public void run() {
                //一旦调用此任务的线程获得了CPU就会执行这个任务的run方法
                activityDao.updateState(entity.getId());
                timer.cancel();//退出任务调度(后续线程也会销毁)
            }
        }, entity.getEndTime());//按指定时间执行任务.
        return rows;
    }
}
